package com.cgnpc.scp.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

public class RequestUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(RequestUtil.class);

    /**
     * 定制的打印调试信息
     */
    public static final String HEADER_CUSTOM_PRINT_DEBUG_INFO = "custom-printDebugInfo";

    /**
     * 定制的下载调试信息
     */
    public static final String HEADER_CUSTOM_DOWNLOAD_TEST_FILE = "custom-downloadTestFile";

    public static String getRequestPrefixPath(HttpServletRequest request) {
        StringBuffer requestURL = request.getRequestURL();
        String requestURI = request.getRequestURI();
        return requestURL.substring(0, requestURL.length() - requestURI.length());
    }

    public static String getHeaderInfo(HttpServletRequest request) {
        String sbReqHeader = "";
        Enumeration<String> enumerationHeaderNames = request.getHeaderNames();

        while (enumerationHeaderNames.hasMoreElements()) {
            String reqHeaderName = enumerationHeaderNames.nextElement();
            String reqHeaderValue = request.getHeader(reqHeaderName);

            sbReqHeader += "\n"
                    + "header.name:[" + reqHeaderName + "];"
                    + "header.value:[" + reqHeaderValue + "]"
            ;
        }

        return sbReqHeader;
    }

}
